package com.zoe.springcloud.config;/*
 *@date 2019/12/14 0014
 */

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.CodeSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @author xjr
 * @title: 入参拦截处理
 * @date 2019/12/01
 */
@Component
@Aspect
@Slf4j
public class ParamsHandler {
    private final JSONObject jsonObject=new JSONObject();

    @Value("${server.port}")
    private String serverPort;

    @Value("${provider.name}")
    public String providerName;
    @Pointcut(value = "execution(* com.zoe.springcloud.web.*.*(..))")
    public void pointcut(){

    }

    @Around(value = "pointcut()")
    public String around(ProceedingJoinPoint pjp) throws Throwable {
        Object result=pjp.proceed();
        jsonObject.put("provider-port",serverPort);
        jsonObject.put("provider-name",providerName);
        Map<String,Object> message=new HashMap<String,Object>();
        Object[] paramValues = pjp.getArgs();
        String[] paramNames = ((CodeSignature)pjp.getSignature()).getParameterNames();
        for (int i=0;i<paramNames.length;i++){
            message.put(paramNames[i],paramValues[i]);
        }
        jsonObject.put("params",message);
        jsonObject.put("result",result==null?"执行成功":result.toString());
        return jsonObject.toJSONString();


    }


}
